Author:SAI K
Core Java Tutorial
The do-while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. Unlike the while loop, the do-while loop guarantees that the code block is executed at least once before the condition is tested. This makes the do-while loop particularly useful for scenarios where you need to ensure that the code block executes at least once.
A do-while loop repeatedly executes a block of code as long as the specified condition evaluates to true. The key difference from a while loop is that the condition is evaluated after the loop body, ensuring that the loop body is executed at least once.
do {
// body of loop
} while (condition);
public class SimpleDoWhileLoop {
public static void main(String[] args) {
int count = 1;
do {
System.out.println("Count: " + count);
count++;
} while (count <= 5);
}
}
Explanation: This loop prints the numbers from 1 to 5. The body of the loop is executed first, and then the condition count <= 5 is checked. Since the condition is true, the loop continues to execute until count exceeds 5.
public class InfiniteDoWhileLoop {
public static void main(String[] args) {
do {
System.out.println("This is an infinite loop");
} while (true);
}
}
Explanation: This loop will print "This is an infinite loop" indefinitely because the condition true never changes.
public class DoWhileWithBreak {
public static void main(String[] args) {
int count = 1;
do {
if (count == 5) {
break;
}
System.out.println("Count: " + count);
count++;
} while (count <= 10);
}
}
Explanation: This loop prints numbers from 1 to 4. When count equals 5, the break statement exits the loop.
public class DoWhileWithContinue {
public static void main(String[] args) {
int count = 0;
do {
count++;
if (count % 2 == 0) {
continue;
}
System.out.println("Odd number: " + count);
} while (count < 10);
}
}
Explanation: This loop prints odd numbers from 1 to 9. The continue statement skips the even numbers.
public class NestedDoWhileLoop {
public static void main(String[] args) {
int i = 1;
do {
int j = 1;
do {
System.out.println("i: " + i + ", j: " + j);
j++;
} while (j <= 3);
i++;
} while (i <= 3);
}
}
Explanation: This loop prints pairs of i and j values, iterating over all combinations of i and j from 1 to 3.
import java.util.Scanner;
public class InputValidation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Enter a positive number: ");
number = scanner.nextInt();
if (number <= 0) {
System.out.println("Invalid input. Please try again.");
}
} while (number <= 0);
System.out.println("You entered: " + number);
scanner.close();
}
}
Explanation: This loop repeatedly prompts the user to enter a positive number until a valid input is provided.
The do-while loop in Java is a powerful control flow statement for performing repeated tasks based on a condition. Unlike the while loop, the do-while loop guarantees that the code block is executed at least once. Understanding how to use the do-while loop effectively, including its variations with break and continue statements, as well as nested loops, is essential for writing robust and efficient Java programs.